package com.example.demo.controller;

import com.example.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

@RestController
public class DemoController {

    @Autowired
    private DemoService demoService;


    /**
     * 2003或以下版本Excel导出
     * @param response
     * @throws Exception
     */
    @GetMapping(value = "/demo/export/HSSF")
    public void excelExportByHSSF(HttpServletResponse response) throws Exception {
        byte[] data = demoService.excelExportByHSSF();
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=HSSF.xls");
        response.setContentLength(data.length);
        response.getOutputStream().write(data);
        response.getOutputStream().flush();
    }

    /**
     * 2007-2010版本Excel导出
     * @param response
     * @throws Exception
     */
    @GetMapping(value = "/demo/export/XSSF")
    public void excelExportByXSSF(HttpServletResponse response) throws Exception {
        byte[] data = demoService.excelExportByXSSF();
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=XSSF.xls");
        response.setContentLength(data.length);
        response.getOutputStream().write(data);
        response.getOutputStream().flush();
    }

    /**
     * 百万级数据Excel导出
     * @param response
     * @throws Exception
     */
    @GetMapping(value = "/demo/export/SXSSF")
    public void excelExportBySXSSF(HttpServletResponse response) throws Exception {
        byte[] data = demoService.excelExportBySXSSF();
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=SXSSF.xls");
        response.setContentLength(data.length);
        response.getOutputStream().write(data);
        response.getOutputStream().flush();
    }

}